//---------------------------------------------------------------------------
//   Author: Chuck Kelly,
//           Monroe County Community College
//           http://www.monroeccc.edu/ckelly
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "HostF.h"
#include "net.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
THostFrm *HostFrm;

extern int          iPort;              // Port to receive on
extern DWORD        dwLength;           // Length of receiving buffer
extern WSADATA      wsd;
extern SOCKET       s;
extern char         *recvbuf;
extern char         *sendbuf;
extern int          ret;
extern DWORD        dwSenderSize;
extern SOCKADDR_IN  remote, local;
extern bool gameServer;                 // true when we are server
extern bool twoPlayer;                  // true when two player game
extern bool gameOn;                     // true when game is running
extern char message[80];                // status messages

bool waiting = true;

//---------------------------------------------------------------------------
__fastcall THostFrm::THostFrm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall THostFrm::Timer1Timer(TObject *Sender)
{
  int ret;

  // Read the datagrams
  int readStatus = readData(recvbuf, sizeof(CLIENT_ID));

  if (readStatus == NET_ERROR) {
    HostMemo->Lines->Add(message);        // display message
    return;
  } else if (readStatus == NET_OK) {
    // if client connected
    if (strncmp(recvbuf, CLIENT_ID, sizeof(CLIENT_ID)) == 0) {
      sprintf(message, "Player connected.");
      HostMemo->Lines->Add(message);    // display

      // send SERVER_ID to client
      strncpy(sendbuf, SERVER_ID, sizeof(SERVER_ID));
      if ( sendData(sendbuf, sizeof(SERVER_ID)) == NET_ERROR) {
        HostMemo->Lines->Add(message);        // display message
        return;
      }

      // Set up
      HostMemo->Lines->Add("Press Start to begin game.");
      HostStartBtn->Enabled = true;     // enable Start button
      Timer1->Enabled = false;          // stop looking for packets
    }
  }
}

//---------------------------------------------------------------------------
void __fastcall THostFrm::HostCancelBtnClick(TObject *Sender)
{
  Close();
}

//---------------------------------------------------------------------------
void __fastcall THostFrm::FormClose(TObject *Sender, TCloseAction &Action)
{
  if (!gameServer) {            // if not game server then close socket
    Timer1->Enabled = false;
    closeSockets();
  }
}

//---------------------------------------------------------------------------
void THostFrm::CloseSocket()
{
  if (gameServer) {            // if game server then close socket
    closeSockets();
  }
}

//---------------------------------------------------------------------------
void __fastcall THostFrm::HostStartBtnClick(TObject *Sender)
{
  // send SERVER_START to client
  strncpy(sendbuf, SERVER_GO, sizeof(SERVER_GO));
  sendData(sendbuf, sizeof(SERVER_GO));

  twoPlayer = false;            // network game
  gameServer = true;            // we are game server
  gameOn = true;                // begin game

  Close();
}
//---------------------------------------------------------------------------

void __fastcall THostFrm::FormShow(TObject *Sender)
{
  char localIP[16];     // storage for local IP address

  HostMemo->Clear();    // clear old messages

  // ----- Initialize network stuff -----
  if (netInit() == NET_ERROR) {
    HostMemo->Lines->Add(message);
    return;
  }

  // Setup network for use as server
  if (setNetServer() == NET_ERROR) {
    HostMemo->Lines->Add(message);        // display message
    return;
  }

  // Display the IP address of this computer
  strncpy(localIP,getLocalIP(),sizeof(localIP));
  MyIP->Text = localIP;

  HostMemo->Lines->Add("Network setup complete.");
  HostMemo->Lines->Add("Waiting for player to connect.");

  Timer1->Enabled = true;

}
//---------------------------------------------------------------------------

